﻿/*
 * Last review by   : Yaser
 * Last review date : JUL-25-09 
 */
using System;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using System.Text;
using ERP.Framework.Components.SerializationHelper;
using ERP.Framework.Components.DatabaseHelper;
using ERP.Business.DataTypes.Company;
using ERP.Framework.Components.Utilities.TracingHelper;
using ERP.Framework.Components.DatabaseHelper.Providers;

namespace ERP.Business.Company
{
    /// <summary>
    /// View or change Company's information.
    /// </summary>
    public abstract class InformationManager
    {
        private const string TRACING_CATEGORY = "ERP.Business.CompanyManagers.InformationManager";
        /// <summary>
        /// Get information Stored procedure name
        /// </summary>
        private const string SP_COMPANY_INFO_GET = "SP_COMPANY_INFO_GET";
        /// <summary>
        /// table name
        /// </summary>
        private const string COMPANY_TABLE_NAME = "Company";
        /// <summary>
        /// Save information stored procedure name
        /// </summary>
        private const string SP_COMPANY_INFO_UPDATE_INSERT = "SP_COMPANY_INFO_UPDATE_INSERT";


        /// <summary>
        /// Retrieves Company's information from database
        /// </summary>
        /// <returns>Company's information as <see cref="ERP.Business.DataTypes.Company.Company"/></returns>
        public static DataTypes.Company.Company GetCompanyInformation()
        {
            TraceUtil.Write(TRACING_CATEGORY, "GetCompanyInformation : Start", DateTime.Now);
            GeneralDatabaseProvider con = new GeneralDatabaseProvider(DatabaseConstants.AdministrationDatabase);
            DataTable table = con.Read(ERP.Framework.Components.DatabaseHelper.CommandType.StoredProcedure, SP_COMPANY_INFO_GET, null);

            DataTypes.Company.Company cmpny = null;
            if (table != null && table.Rows.Count > 0)
            {
                table.TableName = COMPANY_TABLE_NAME;
                StringBuilder sb = new StringBuilder();
                using (XmlWriter x = XmlWriter.Create(sb))
                {
                    table.WriteXml(x);
                }
                cmpny = (DataTypes.Company.Company)XmlSerializingManager.DeserializeObject<DataTypes.Company.Company>(sb.ToString());
            }

            TraceUtil.Write(TRACING_CATEGORY, "GetCompanyInformation : End", DateTime.Now);
            return cmpny;
        }

        /// <summary>
        /// Update saved Company's information in database
        /// </summary>
        /// <param name="value">New company's information as <see cref="ERP.Business.DataTypes.Company.Company"/></param>
        /// <returns>true if updated.</returns>
        public static bool SaveCompanyInformation(DataTypes.Company.Company value)
        {
            TraceUtil.Write(TRACING_CATEGORY, "SaveCompanyInformation : Start", DateTime.Now);
            GeneralDatabaseProvider con = new GeneralDatabaseProvider(DatabaseConstants.AdministrationDatabase);

            ParametersCollection p = new ParametersCollection();
            p.AddParameter(new Parameter("CID", value.ID, ParameterType.String));
            p.AddParameter(new Parameter("Name", value.Name, ParameterType.String));
            p.AddParameter(new Parameter("Address", value.Address, ParameterType.String));
            p.AddParameter(new Parameter("PhoneNumber", value.PhoneNumber, ParameterType.String));
            p.AddParameter(new Parameter("FaxNumber", value.FaxNumber, ParameterType.String));
            p.AddParameter(new Parameter("EmailAddress", value.EmailAddress, ParameterType.String));
            p.AddParameter(new Parameter("WebSite", value.WebSite, ParameterType.String));
            p.AddParameter(new Parameter("SocialSecurityNumber", value.SocialSecurityNumber, ParameterType.String));
            p.AddParameter(new Parameter("TaxCode", value.TaxCode, ParameterType.String));

            int count = con.Save(ERP.Framework.Components.DatabaseHelper.CommandType.StoredProcedure, SP_COMPANY_INFO_UPDATE_INSERT, p);

            TraceUtil.Write(TRACING_CATEGORY, "SaveCompanyInformation : End", DateTime.Now);
            return count > 0;
        }
    }
}
